package com.sp.scmdb.serviceImpl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sp.scmdb.common.CommonParamter;
import com.sp.scmdb.dao.FileUploadDao;
import com.sp.scmdb.entity.FileUpload;
import com.sp.scmdb.service.FileUploadService;
import com.sp.scmdb.service.LogService;

/**
 * 
 * @author Jianq Shunming
 * 2012-12-06
 * 附件上传操作
 */

@Service("fileUploadService")
public class FileUploadServiceImpl implements FileUploadService {
	
	Log log = LogFactory.getLog(FileUploadServiceImpl.class);
	
	private FileUploadDao fileUploadDao;
	private LogService logs;
	
	public void setFileUploadDao(FileUploadDao fileUploadDao) {
		this.fileUploadDao = fileUploadDao;
	}

	public void setLogs(LogService logs) {
		this.logs = logs;
	}
	
	@Override
	@Transactional
	public String createFileUpload(FileUpload fileUpload) {
		String result = CommonParamter.CHECK_FAIL;
		try {
			fileUploadDao.save(fileUpload);
			result = CommonParamter.CHECK_SUCCESS;
			log.info("上传附件插入数据库成功");
		} catch (Exception e) {
			result = CommonParamter.CHECK_FAIL;
			log.error("上传附件插入数据库错误" + e.getMessage());
		}
		return result;
	}

	@Override
	public String delFileUpload(FileUpload fileUpload) {
		String result = CommonParamter.CHECK_FAIL;
		try {
			fileUploadDao.delete(fileUpload);
			result = CommonParamter.CHECK_SUCCESS;
			log.info("删除附件成功");
		} catch (Exception e) {
			result = CommonParamter.CHECK_FAIL;
			log.error("删除附件失败" + e.getMessage());
		}
		return result;
	}

	@Override
	public String editFileUpload(FileUpload fileUpload) {
		String result = CommonParamter.CHECK_FAIL;
		try {
			fileUploadDao.update(fileUpload);
			result = CommonParamter.CHECK_SUCCESS;
			log.info("编辑附件成功");
		} catch (Exception e) {
			result = CommonParamter.CHECK_FAIL;
			log.error("编辑附件失败" + e.getMessage());
		}
		return result;
	}

	@Override
	public FileUpload getFileUpload(String sysId) {
		return fileUploadDao.getBySysId(sysId);
	}

	@Override
	public List<FileUpload> getListByEntityId(String entityId) {
		return fileUploadDao.queryHql("from FileUpload where entityId = '" + entityId + "' ");
	}

}
